package com.java.jiuyang.commons.service;


import com.java.jiuyang.commons.dao.SysTerminalManagerDao;
import com.java.jiuyang.commons.entity.terminal.SysTerminalManager;
import com.java.jiuyang.commons.entity.terminal.SysUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @ClassName: SysTerminalManagerServiceImpl
 * @Description:
 * @Version: v1.0.0
 * @Author: Fu Hao
 * @Date: 2019/10/15 0015 下午 6:07
 * Modification History:
 * Date           Author      Version     Description
 * -------------------------------------------------------------
 * 2019/10/15 0015        Fu Hao      v1.0.0         创建
 */
@Service("SysTerminalManagerService")
public class SysTerminalManagerServiceImpl implements SysTerminalManagerService {

   // private static Logger logger = LoggerFactory.getLogger(SysMachineService.class);
   /* @Autowired
    private SystemLanguageService systemLanguageService;*/

    @Autowired
    private SysTerminalManagerDao sysTerminalManagerDao;

    @Override
    public List<SysTerminalManager> findManager(String id) {
        /*if (StringUtils.isEmpty(id)){
            throw new BaseSystemException(systemLanguageService.getMessage(SystemResultEnums.PARAMS_VANNOT_BE_BULL.getMsg()),
                    SystemResultEnums.PARAMS_VANNOT_BE_BULL.getCode());
        }*/
        List<SysTerminalManager> userId=sysTerminalManagerDao.findUserId(id);
        return sysTerminalManagerDao.findManager(userId);

    }

    /*终端店主信息修改*/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updTerminalManager(String id,String userName,String phone) {
       /* if (StringUtils.isEmpty(id) || StringUtils.isEmpty(userName)|| StringUtils.isEmpty(phone)){
            throw new BaseSystemException(systemLanguageService.getMessage(SystemResultEnums.CHECK_NOT_PASS.getMsg()),
                    SystemResultEnums.CHECK_NOT_PASS.getCode());
        }*/
        String regexNumber="^1[3|4|5|7|8][0-9]\\d{8}$";
        String regexName="^[\\u4e00-\\u9fa5]{2,4}$";
        Boolean isCheckNumber=regexNumber.matches(userName);
        Boolean isCheckName=regexName.matches(phone);
        /*if (!isCheckName||!isCheckNumber){
            throw new BaseSystemException(systemLanguageService.getMessage(SystemResultEnums.CHECK_NOT_PASS.getMsg()),
                    SystemResultEnums.CHECK_NOT_PASS.getCode());
        }*/
        return sysTerminalManagerDao.updTerminalManager(id,userName,phone);
    }

    /*终端店主信息删除*/
    @Override
    public int delTerminalById(@Param("id") String id) {
        //if (!StringUtils.isEmpty(id)){
            return sysTerminalManagerDao.delTerminalById(id);
        //}
        /*throw new BaseSystemException(systemLanguageService.getMessage(SystemResultEnums.PARAMS_VANNOT_BE_BULL.getMsg()),
                SystemResultEnums.PARAMS_VANNOT_BE_BULL.getCode());*/
    }

    /*添加终端店主信息*/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String insertTerminal(String id, String terminalId, SysUser sysUser) {
        /*if (StringUtils.isEmpty(terminalId)|| StringUtils.isEmpty(sysUser.getName())|| StringUtils.isEmpty(sysUser.getPhone())){
            throw new BaseSystemException(systemLanguageService.getMessage(SystemResultEnums.PARAMS_VANNOT_BE_BULL.getMsg()),
                    SystemResultEnums.PARAMS_VANNOT_BE_BULL.getCode());
        }*/
        String regexNumber="^[1][3,4,5,7,8][0-9]{9}$";
        String regexName="^[\\u4e00-\\u9fa5]{2,4}$";
        Boolean isEmptyNumber=regexNumber.matches(sysUser.getPhone());
        Boolean isEmptyName=regexName.matches(sysUser.getName());
       /* if (!isEmptyName||!isEmptyNumber){
            throw new BaseSystemException(systemLanguageService.getMessage(SystemResultEnums.CHECK_NOT_PASS.getMsg()),
                    SystemResultEnums.CHECK_NOT_PASS.getCode());
        }*/
        //先插入用户表，通过对象获取主键id
        sysTerminalManagerDao.insertSysUser(sysUser);
        String userId=sysUser.getId();
        //将获取的用户主键插入店主表中做关联
        return sysTerminalManagerDao.insertTerminal(id,terminalId,userId);
    }

}
